﻿using Rsd.Dudu;
using Rsd.Dudu.Web;
using Rsd.Dudu.Web.Core;
using Rsd.Dudu.Web.MVC;

using Rsd.Faith.Core;
using Rsd.Faith.Fiscal.Core;
using Rsd.Faith.Fiscal.Services;
using Rsd.Faith.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace Rsd.Faith.Fiscal.Controllers 
{
    /// <summary>
    /// 业务 操作 引发的入账 
    /// 
    /// 第一步：处理缴存； 第二步：处理使用 ； 第三步 处理结息；
    /// </summary>
    public class FiscalController : Rsd.Faith.Core.FaithController
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [ApiService("Index",false)]
        override public object Index()
        {
            //MyType.InitialTable<FiscalKaiFaShangJiaoCunShenQingModel>(Dudu.DBServices.TableExistOperationType.AlterAndDropColumn);
            //MyType.InitialTable<FiscalKaiFaShangJiaoCunShenQingSubModel>(Dudu.DBServices.TableExistOperationType.AlterAndDropColumn);

            return new { };
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="faPiaoHaoMa"></param>
        /// <returns></returns>
        [PramaRule("faPiaoHaoMa", "发票号码(faPiaoHaoMa)", typeof(string))]
        [ApiService("住宅(ZZ)维修资金缴存确认到账")]
        public object JiaoCunQueRenZZ(string faPiaoHaoMa)
        {
            var objFP = this.GetObject<FiscalPiaoJuXinXiBiaoModel>(obj => obj.FaPiaoBianHao == faPiaoHaoMa);
            if (objFP == null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00003, null, faPiaoHaoMa);
            }

            if (objFP.ShiJiaoJinE > 0)
            { }
            else
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00009, null, faPiaoHaoMa);
            }
            var fangwudaima = objFP.FangWuDaiMa;
            var xiaoQuBianHao = objFP.XiaoQuBianHao;

            var xiaoqu = MyType.GetObject<InfoXiaoQuWuYeBiaoModel>(obj => obj.BianHao == xiaoQuBianHao);

            if (xiaoqu == null)
            {
                //小区不存在
                ErrorCodesType.F00023.SubmitError(faPiaoHaoMa, xiaoQuBianHao);
                return null;

            }
            var fangWu = this.GetObject<InfoFangWuXinXiBiaoModel>(obj => obj.BianHao == fangwudaima);

            if (fangWu == null)
            {
                ErrorCodesType.F00006.SubmitError(fangwudaima);
                return null;
            }

            if (xiaoQuBianHao != fangWu.XiaoQuBianHao)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00012, null, xiaoQuBianHao, fangWu.XiaoQuBianHao);
            }
            if (string.IsNullOrEmpty(objFP.SuoShuQi))
            {
                objFP.SuoShuQi = objFP.JiaoNaRiQi.Year.ToString();
            }
            objFP.ZhuangTai = FaPiaoZhuangTai.SH;
            objFP.SetState(Dudu.BizModel.ObjectStateType.Update);
            this.SaveObject<FiscalPiaoJuXinXiBiaoModel>(objFP);
            return objFP;
        }

        /// <summary>
        /// 住宅(ZZ)缴存入账
        /// </summary>
        [PramaRule("faPiaoHaoMa", "发票号码(faPiaoHaoMa)", typeof(string))]
        [ApiService("住宅(ZZ)维修资金缴存入账",true)]
        public object JiaoCunRuZhangZZ([Service] IBookService bookService, [Service] IFiscalService  fiscalService,string faPiaoHaoMa)
        {

            var objFP = this.GetObject<FiscalPiaoJuXinXiBiaoModel>(obj=>obj.FaPiaoBianHao == faPiaoHaoMa);
            if (objFP == null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00003, null, faPiaoHaoMa);
            }
            if (string.IsNullOrEmpty(objFP.SuoShuQi))
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00008, null, faPiaoHaoMa);
            }
            if (objFP.ShiJiaoJinE > 0)
            { }
            else
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00009, null, faPiaoHaoMa);
            }
            var fangwudaima = objFP.FangWuDaiMa;
            var xiaoQuBianHao = objFP.XiaoQuBianHao;

            var xiaoqu = MyType.GetObject<InfoXiaoQuWuYeBiaoModel>(obj => obj.BianHao == xiaoQuBianHao);

            if (xiaoqu == null)
            {
                //小区不存在
                ErrorCodesType.F00023.SubmitError(faPiaoHaoMa,xiaoQuBianHao);
                return null;

            }
            var fangWu = this.GetObject<InfoFangWuXinXiBiaoModel>(obj => obj.BianHao == fangwudaima);
              
            if (fangWu == null)
            {
                ErrorCodesType.F00006.SubmitError(fangwudaima);
                return null;
            }
             
            if (xiaoQuBianHao != fangWu.XiaoQuBianHao)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00012, null, xiaoQuBianHao, fangWu.XiaoQuBianHao);
            }
            //创建分户账
            var fangHuAccount = bookService.NewFenHuAccount(fangWu.BianHao, fangWu.XiaoQuBianHao, objFP.JiaoNaRiQi);
           
            if (fangWu.ZhangHuBianHao != fangHuAccount.ZhangHuBianHao)
            {
                fangWu.ZhangHuBianHao = fangHuAccount.ZhangHuBianHao;
                fangWu.SetState(Dudu.BizModel.ObjectStateType.Update);
                fangWu = this.SaveObject<InfoFangWuXinXiBiaoModel>(fangWu);
            }
            //创建单元账
            var danYuanAccount = bookService.NewDanYuanAccount(fangWu.DanYuanHao, fangWu.LouDongHao, fangWu.XiaoQuBianHao, objFP.JiaoNaRiQi);
            //创建楼栋账
            var louDongAccount = bookService.NewLouDongAccount(fangWu.LouDongHao, fangWu.XiaoQuBianHao, objFP.JiaoNaRiQi);
             //创建小区账
            var xiaoQuAccount = bookService.NewXiaoQuAccount(fangWu.XiaoQuBianHao, xiaoqu.MingCheng, objFP.JiaoNaRiQi);

            if (objFP.ZhuangTai != FaPiaoZhuangTai.RZ)
            {
                objFP.ZhuangTai = FaPiaoZhuangTai.RZ;
                objFP.SetState(Dudu.BizModel.ObjectStateType.Update);
                this.SaveObject<FiscalPiaoJuXinXiBiaoModel>(objFP);
            }
            
            return fiscalService.DepositRecordingZZ(bookService, objFP, xiaoQuAccount, louDongAccount, danYuanAccount, fangHuAccount);
             
        }

        /// <summary>
        /// 电梯消防(DT)缴存入账：入分户明细账 和 专户（可选）
        /// </summary>
        [PramaRule("faPiaoHaoMa", "发票号码(faPiaoHaoMa)", typeof(string))]
        [ApiService("电梯消防(DT)维修资金缴存入账",true)]
        public object JiaoCunRuZhangDT(string faPiaoHaoMa)
        {
            var objFP = this.GetObject<FiscalPiaoJuXinXiDtBiaoModel>(faPiaoHaoMa);

            if (objFP == null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00003,null, faPiaoHaoMa);
            }
            var service = this.GetService<IFiscalService>();
            if (objFP != null)
            {
                objFP.ZhuangTai =  FaPiaoZhuangTai.RZ;
                this.SaveObject<FiscalPiaoJuXinXiDtBiaoModel>(objFP);
            }
            //
            
            return service.DepositRecordingDT(objFP );
        }

        /// <summary>
        /// 划拨入账 根据划拨信息 可以核算小区账
        /// </summary>
        [PramaRule("shenQingDanHao", "申请单号(shenQingDanHao)")]
        [ApiService("划拨(本金账户)入账")]
        public object HuaBoRuZhang([Service] IBookService bookService, [Service] IFiscalService fiscalService,  string shenQingDanHao)
        {
            var shenQing = this.GetObject<UsingZiJinShiYongShenQingBiaoModel>(obj => obj.ShenQingDanHao == shenQingDanHao);
            if (shenQing == null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00013, null, shenQingDanHao);
            }
            var huaBo = this.GetObject<UsingZiJinShiYongHuaBoBiaoModel>(obj => obj.HuaBoBianHao == shenQing.HuaBoBianHao);
            if (huaBo == null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00014, null, shenQing.HuaBoBianHao);
            }
            var xiaoQu = this.GetObject<InfoXiaoQuWuYeBiaoModel>(obj => obj.BianHao == shenQing.XiaoQuBianHao);
            if (xiaoQu == null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00015, null, shenQing.XiaoQuBianHao);
            }

            object rs = 0;
            switch (huaBo.FuKuanZhangHu)
            {
                case FuKuanZhangHu.BJ: //从本金账户拨款
                    {
                        //根据划拨信息 可准确核算 资金总账 小区账
                        rs = fiscalService.FundingRecording(bookService, huaBo);
                        break;
                    } 
                case FuKuanZhangHu.TC://从总账统筹账户拨款
                    {
                        //总账增值账户 新增支出
                        //
                        rs = "付款账户为总账统筹账户，请求Bank/ShiYongBoHua接口替代当前接口"; 
                        break;
                    }
                case FuKuanZhangHu.XQTC://从小区统筹账户拨款
                    {
                        rs = "付款账户为总账统筹账户，请求Bank/ShiYongBoHua接口替代当前接口";
                        break;
                    }
                case FuKuanZhangHu.LDTC://从楼栋统筹账户拨款
                    {
                        rs = "付款账户为总账统筹账户，请求Bank/ShiYongBoHua接口替代当前接口";
                        break;
                    }
                case FuKuanZhangHu.DYTC://从单元统筹账户拨款
                    {
                        rs = "付款账户为总账统筹账户，请求Bank/ShiYongBoHua接口替代当前接口";
                        break;
                    }
                default:
                    {
                        rs = "未指定付款账户，无法拨付入账";
                        break;
                    }
            }
            //

            return rs;
        }

        /// <summary>
        /// 分摊入明细帐:支出 EX:sq20160805005
        /// </summary>
        /// <param name="fiscalService"></param>
        /// <param name="shenQingDanHao"></param>
        /// <returns></returns>
        [PramaRule("shenQingDanHao", "申请单号(shenQingDanHao)", typeof(string))]
        [ApiService("分摊入明细帐", true)]
        public object FenTanRuZhangMX([Service] IBookService bookService, [Service] IFiscalService fiscalService,  string shenQingDanHao)
        {
            var count = 0;

            var shenQing = this.GetObject<UsingZiJinShiYongShenQingBiaoModel>(obj => obj.ShenQingDanHao == shenQingDanHao);
            if (shenQing == null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00013, null, shenQingDanHao);
            }
            var huaBo = this.GetObject<UsingZiJinShiYongHuaBoBiaoModel>(obj => obj.ShenQingDanHao == shenQingDanHao);
            if (huaBo == null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00014, null, shenQingDanHao);
            }
            if (huaBo.FuKuanZhangHu != FuKuanZhangHu.BJ)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00026, null, shenQingDanHao); 
            }
           
            try
            {
                fiscalService.SharingRecordingMX(bookService, huaBo);
            }
            catch (System.Exception ex)
            {
                ErrorCodesType.F00019.SubmitError(shenQingDanHao, ex.ToString());
            }
            
            return count;
        }

        /// <summary>
        /// 分摊入分户帐:支出 sq20160805005
        /// </summary>
        [PramaRule("shenQingDanHao", "申请单号(shenQingDanHao)", typeof(string))]
        [ApiService("分摊入分户帐",true)]
        public object FenTanRuZhangFH([Service] IFiscalService fiscalService, string shenQingDanHao)
        {
            var count = 0;
            var shenQing = this.GetObject<UsingZiJinShiYongShenQingBiaoModel>(obj => obj.ShenQingDanHao == shenQingDanHao);
            if (shenQing == null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00013, null, shenQingDanHao);
            }
            var huaBo = this.GetObject<UsingZiJinShiYongHuaBoBiaoModel>(obj => obj.ShenQingDanHao == shenQingDanHao);
            if (huaBo == null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00014, null, shenQingDanHao);
            }
            if (huaBo.FuKuanZhangHu != FuKuanZhangHu.BJ)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00026, null, shenQingDanHao);
            }

            
            try
            {
                count += fiscalService.SharingRecordingFH(huaBo);

            }
            catch (System.Exception ex)
            {
                ErrorCodesType.F00020.SubmitError(shenQingDanHao, ex.ToString());
            }

            //修改划拨数据状态为 已入账
            huaBo.ZhuangTai = ZiJinHuaBoZhuangTai.RZ;
            huaBo.SetState(Dudu.BizModel.ObjectStateType.Update);
            this.SaveObject<UsingZiJinShiYongHuaBoBiaoModel>(huaBo);

            return count;
        }


        /// <summary>
        /// 检查房屋信息(如不存在添加)
        /// </summary>
        /// <param name="fiscalService"></param>
        /// <param name="shenQingDanHao"></param>
        /// <returns></returns>
        [PramaRule("shenQingDanHao", "申请单号(shenQingDanHao)", typeof(string))]
        [ApiService("检查房屋信息(如不存在添加)", true)]
        public object FenTanZhangHuJianCha([Service] IBookService bookService, [Service] IFiscalService fiscalService, string shenQingDanHao)
        {
            var count = 0;

            var shenQing = this.GetObject<UsingZiJinShiYongShenQingBiaoModel>(obj => obj.ShenQingDanHao == shenQingDanHao);
            if (shenQing == null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00013, null, shenQingDanHao);
            }
            var huaBo = this.GetObject<UsingZiJinShiYongHuaBoBiaoModel>(obj => obj.ShenQingDanHao == shenQingDanHao);
            if (huaBo == null)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00014, null, shenQingDanHao);
            }
            if (huaBo.FuKuanZhangHu != FuKuanZhangHu.BJ)
            {
                return new ErrorResult<ErrorCodesType>(ErrorCodesType.F00026, null, shenQingDanHao);
            }
            //检查房屋信息是否存在（分摊数据存在 未缴费的房屋）
            try
            {
                fiscalService.NewFangWuXinXi(bookService, huaBo);
            }
            catch (System.Exception ex)
            {
                ErrorCodesType.F00037.SubmitError(shenQingDanHao, ex.ToString());
            }

            return count;
        }
         
        /// <summary>
        /// 将划拨，分摊，入账 三个动作 合并为一次API 请求 
        /// </summary>
        /// <param name="fiscalService"></param>
        /// <param name="bookService"></param>
        /// <param name="shenQingDanHao"></param>
        /// <returns></returns>
        [PramaRule("shenQingDanHao", "申请单号(shenQingDanHao)")]
        [ApiService("使用入账（划拨，分摊，入账）")]
        public object ShiYongRuZhang([Service] IFiscalService fiscalService, [Service] IBookService bookService, string shenQingDanHao)
        {
            this.HuaBoRuZhang(bookService, fiscalService,shenQingDanHao);
            this.FenTanRuZhangMX(bookService, fiscalService, shenQingDanHao);
            this.FenTanRuZhangFH(fiscalService,  shenQingDanHao);

            return new { };
        }

    }
}
